package com.lb.jwitter.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTKhoiNganhMonHoc;
import com.lb.jwitter.service.TblCTDTNhomMHTuChonMonHoc;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKHocVien;
import com.lb.jwitter.service.TblDKMHDotDKHocVienService;
import com.lb.jwitter.service.TblDKMHDotDKService;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVien;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVienService;
import com.lb.jwitter.service.TblMenu;
import com.lb.jwitter.service.TblMenuService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblQuyenMenu;
import com.lb.jwitter.service.TblQuyenMenuService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblThongTinHocVienMonHocService;
import com.lb.jwitter.service.TblThongTinMonHoc;
import com.lb.jwitter.service.TblThongTinMonHocService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.CTDT;
import com.lb.jwitter.util.KetQuaDangKy;
import com.lb.jwitter.util.NhomMonHocTuChon;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class GetDanhSachMonHocDangKyAction {

	private TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien;
	public TblDKMHTaiKhoanHocVien getTblDKMHTaiKhoanHocVien() {
		return tblDKMHTaiKhoanHocVien;
	}

	public void setTblDKMHTaiKhoanHocVien(
			TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien) {
		this.tblDKMHTaiKhoanHocVien = tblDKMHTaiKhoanHocVien;
	}

	private int dotDKId;

	public int getDotDKId() {
		return dotDKId;
	}

	public void setDotDKId(int dotDKId) {
		this.dotDKId = dotDKId;
	}
	
	private List<TblThongTinMonHoc> tblThongTinMonHocs;
	private TblThongTinMonHocService tblThongTinMonHocService;

	public List<TblThongTinMonHoc> getTblThongTinMonHocs() {
		return tblThongTinMonHocs;
	}

	public void setTblThongTinMonHocs(List<TblThongTinMonHoc> tblThongTinMonHocs) {
		this.tblThongTinMonHocs = tblThongTinMonHocs;
	}
	
	private List<KetQuaDangKy> ketQuaDangKys;

	public List<KetQuaDangKy> getKetQuaDangKys() {
		return ketQuaDangKys;
	}

	public void setKetQuaDangKys(List<KetQuaDangKy> ketQuaDangKys) {
		this.ketQuaDangKys = ketQuaDangKys;
	}

	private CTDT ctdt;


	public CTDT getCtdt() {
		return ctdt;
	}

	public void setCtdt(CTDT ctdt) {
		this.ctdt = ctdt;
	}

	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	 	 
	public String execute() throws Exception
	{

		Map session = ActionContext.getContext().getSession();
		tblDKMHTaiKhoanHocVien =  (TblDKMHTaiKhoanHocVien) session.get("taiKhoanHocVien");
	     if( tblDKMHTaiKhoanHocVien == null) return "noprivilege";
	     
	    ApplicationContext context = contextProvider.getApplicationContext(); 
	    
	    tblThongTinMonHocService = (TblThongTinMonHocService)  context.getBean("tbl_ThongTin_MonHocClient");

		TblThongTinHocVienMonHocService tblThongTinHocVienMonHocService = (TblThongTinHocVienMonHocService) context.getBean("tbl_ThongTin_HocVien_MonHocClient");
		TblDKMHDotDKService tblDKMHDotDKService = (TblDKMHDotDKService) context.getBean("tbl_DKMH_DotDKClient");
	    
	    if(session.get("dotDKId") != null)
	    	dotDKId = (Integer) session.get("dotDKId");
	    
	    TblDKMHDotDK dotDK = tblDKMHDotDKService.gettblDKMHDotDK(new Long(dotDKId));
	    
		if(tblDKMHTaiKhoanHocVien != null){
			List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
			List<String> columns = new ArrayList<String>();
			
			//get danh sach mon hoc chua dat
			cons.clear();
			{
				ConditionReport con = new ConditionReport();
				con.setCol("hocVienId.id");
				con.setValue1(tblDKMHTaiKhoanHocVien.getHocVienId().getId());
				con.setOperator("eq");
				cons.add(con);
			}
			{
				ConditionReport con = new ConditionReport();
				
				con.setType("String");
				con.setCol("trangThai");
				con.setValue1("FAIL"); 
				con.setOperator("eq");
				cons.add(con);
			}
			
			columns.add("monHocId.id");
			List<Object> monHocFails = (List<Object>) tblThongTinHocVienMonHocService.findColumntblThongTinHocVienMonHocByCondition(cons, columns, 0, 0);

		    String danhSachIdMonHoc = "";
		    for(Object obj : monHocFails){
		    	danhSachIdMonHoc += obj.toString() + ";";
		    }
		    
		    //danh sach mon hoc theo CTDT va hoc ky hien tai
		    ctdt =( CTDT ) session.get("CTDT");
		    
		    int khoaHoc = tblDKMHTaiKhoanHocVien.getHocVienId().getKhoaHoc();
		    int namHienTai = 0;
		    int hocKyTrongNam = 1;
		    //parse khoa hoc
		    try {
			    namHienTai = Integer.parseInt(dotDK.getHocKyId().getNamHoc());
				
			} catch (Exception e) {
			    namHienTai = tblDKMHTaiKhoanHocVien.getHocVienId().getKhoaHoc();
			}

		    //parse hocky trong nam
		    try {
				hocKyTrongNam = Integer.parseInt(dotDK.getHocKyId().getMa().substring(4));
			} catch (Exception e) {

			}
		    
		    int hocKyHienTaiTheoCTDT = (namHienTai- khoaHoc)*2 + hocKyTrongNam;
		    System.out.println("HOCKY TRONGNAM: " + hocKyTrongNam);
		    System.out.println("HOCKY HIENTAI: " + hocKyHienTaiTheoCTDT);
		    //ds mon hoc
		    for(TblCTDTKhoiNganhMonHoc monHoc : ctdt.getMonHocs()){
		    	try {
		    		if(hocKyHienTaiTheoCTDT == Integer.parseInt(monHoc.getHocKy()))
			    		danhSachIdMonHoc += monHoc.getMonHocId().getId() +";";
				} catch (Exception e) {
				}
		    }
//		    //ds mon hoc tu chon
//		    for(NhomMonHocTuChon nhom : ctdt.getNhomMonHocTuChons()){
//		    	
//		    	try {
//		    		if(hocKyHienTaiTheoCTDT == Integer.parseInt(nhom.getNhomTuChon().getHocKy())){
//		    			
//		    			for(TblCTDTNhomMHTuChonMonHoc monHocTC : nhom.getMonHocTuChons()){
//				    		danhSachIdMonHoc += monHocTC.getMonHocId().getId() +";";
//		    			}
//		    		}//end-if
//				} catch (Exception e) {
//				}
//		    }

			cons.clear();
		    { 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("dotDKId.id");
		        con.setValue1(new Long(dotDKId));
		        con.setOperator("eq");
		        cons.add(con);	
		    }
		    { 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("monHocId.id");
		        con.setType("Long");
		        con.setValue1(danhSachIdMonHoc);
		        con.setOperator("in");
		        cons.add(con);	
		    }
		    
		    tblThongTinMonHocs = tblThongTinMonHocService.findtblThongTinMonHocByCondition(cons, 0, 0);
		    ketQuaDangKys = (List<KetQuaDangKy>) session.get("ketQuaDangKys");
			
		}
		return Action.SUCCESS;

	}


	
	
	
	
	
}
